package cn.congzhou.crm.web.controller;


import cn.congzhou.crm.domain.Role;
import cn.congzhou.crm.query.QueryObject;
import cn.congzhou.crm.service.IPermissionService;
import cn.congzhou.crm.service.IRoleService;
import cn.congzhou.crm.utils.ResultJSON;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("/role")
public class RoleController {

    private Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"查看系统角色","role:list"})
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {

        model.addAttribute("ro", roleService.selectByPage(qo));

        return "/role/list";
    }


    @RequestMapping("/input")
    @RequiresPermissions(value = {"编辑角色","role:input"},logical = Logical.OR)
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("entity", roleService.selectByPrimaryKey(id));
        }
        model.addAttribute("permissions",permissionService.selectAll());
        return "/role/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"保存/修改角色","role:saveOrUpdate"},logical = Logical.OR)
    public String saveOrUpdate(Role role,Long[] ids) {
        roleService.saveOrUpdate(role,ids);

        return "redirect:/role/list.do";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"删除角色","role:delete"},logical = Logical.OR)
    public String delete(Long id) {
        roleService.deleteByPrimaryKey(id);

        return "redirect:/role/list.do";
    }

    //==========================添加ligerUI添加的方法============================================//

    @RequestMapping(value = "/toRoleGrid",method = RequestMethod.GET)
    @RequiresPermissions(value = {"查看系统角色","role:query"})
    public String toRoleGrid(){

        return "/role/roleGrid";
    }

    @ResponseBody
    @RequestMapping(value = "/getRoleData",method = RequestMethod.POST)
    @RequiresPermissions(value = {"查看系统角色","role:query"})
    public Object getRoleData(@ModelAttribute("qo")QueryObject qo, HttpServletResponse response){
        PageInfo<Role> pageInfo = roleService.selectByPage(qo);
        String json = JSON.toJSONString(pageInfo);
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            writer.print(json);
            writer.flush();
        } catch (IOException e) {
            logger.error("响应角色信息出错",e);
        }finally {
            if(writer != null){
                try{
                    writer.close();
                }catch (Exception e){}
            }
        }
        return null;
    }

}
